package filter

import (
	"fmt"
	sessionStore "github.com/test/src/github.com/http/session-store"
	"net/http"
)

func Secure(h func(writer http.ResponseWriter, request *http.Request)) func(writer http.ResponseWriter, request *http.Request) {
	return func(writer http.ResponseWriter, request *http.Request) {
		cookie, err := request.Cookie(sessionStore.COOKIE_SESSION_ID)
		if err != nil {
			writer.WriteHeader(http.StatusUnauthorized)
			fmt.Fprint(writer, "not auth")
			return
		}
		sessionId := cookie.Value
		_, ok := sessionStore.SessionMap[sessionId]
		if !ok {
			writer.WriteHeader(http.StatusUnauthorized)
			fmt.Fprint(writer, "not auth")
			return
		}
		h(writer, request)
	}
}
